import * as mobx from 'mobx';
import { observable, action, useStrict, computed, asMap } from 'mobx';
import { getPrintLogBase,getPrintLogData } from '../apis/req';
import { getComsMobx } from '../util/convert';

export class PrintLogStore{

    baseParams = {};
    closeEvent = null;
    
    @observable tabKey = "0";
    @observable topTab = [];
    @observable showSearchAd = false;
    conditions = [];
    @observable form = null;
    @observable tableStore = null;

    constructor(){
        this.setBaseParams = this.setBaseParams;
        this.switchTab = this.switchTab;
        this.loadBaseInfo = this.loadBaseInfo;
        this.doSearch = this.doSearch;
        this.setShowSearchAd = this.setShowSearchAd;
        this.appendFormFields = this.appendFormFields;
        this.clearFormFields = this.clearFormFields;
        this.setCloseEvent = this.setCloseEvent;
        this.resetStatus = this.resetStatus;
    }

    @action
    setBaseParams = (params={}) => {
        const { WeaTableNew } = getComsMobx();
        this.tableStore = new WeaTableNew.TableStore();

        this.baseParams = params;
    }

    switchTab = (key) => {
        this.tabKey = key;
    }

    loadBaseInfo = () => {
        getPrintLogBase(this.baseParams).then(data =>{
            const { tabs,conditions } = data;
            const { WeaForm } = getComsMobx();
            this.form = new WeaForm();
            this.topTab = tabs;
            this.conditions = conditions;
            this.form.initFormFieldObj(conditions);
        });
    }

    doSearch = () => {
        const formParams = this.form && this.form.getFormParams() || {};
        const params = {...this.baseParams, ...formParams, ...{viewcondition:this.tabKey}};
        getPrintLogData(params).then(data =>{
            this.tableStore.getDatas(data.sessionkey);
        });
    }

    setShowSearchAd = (bool) => {
        this.showSearchAd = bool;
    }

    appendFormFields = (value) => {
        this.form && this.form.updateFields(value, false);
    }

    clearFormFields = () => {
        this.form && this.form.resetConditionValue();      //清除查询条件值
    }

    setCloseEvent = (fun) => {
        this.closeEvent = fun;
    }

    resetStatus = () => {
        this.baseParams = {};
        this.tabKey = "0";
        this.showSearchAd = false;
    }

}